home *** CD-ROM | disk | FTP | other *** search
/ Business Shareware / Business Shareware.iso / start / wordproc / me_cd25 / texmode.mut < prev    next >
Encoding:
Text File  |  1992-11-09  |  8.6 KB  |  300 lines

  1. ;;
  2. ;; File: 
  3. ;;   texmode.mut
  4. ;;
  5. ;; Description:
  6. ;;   Rountines to make (La)TeX input more fun.
  7. ;;
  8. ;;   - No attempt has been made to be compatible with GNU emacs or another
  9. ;;     'smart' editor.
  10. ;;
  11. ;;   - Based on the (probably) most frequently used commands of the Quick
  12. ;;     Reference Quide in "LaTeX User's Guide & Reference Manual" by
  13. ;;     Leslie Lamport.
  14. ;;
  15. ;;   - The new paragraph commands assume that both an empty line and a line 
  16. ;;     that starts with `\[beis]` mark the begin of a paragraph (\begin,
  17. ;;     \end, \item, \section). This can be annoying sometimes!
  18. ;;
  19. ;;   - tex-format-paragraph is not as sophisticated as 'adjust-lines' in
  20. ;;     'adjust.mut', but it is much faster and real formatting is assumed
  21. ;;     to be done by (La)TeX.
  22. ;;
  23. ;;   - Since I like to have an almost "normal" keyboard, no attempt has
  24. ;;     been made for very smart key bindings, just type M-\ or C-\ to 
  25. ;;     invoke a kind of LaTeX menu. Maybe command completion for
  26. ;;     LaTeX commands is better.
  27. ;;
  28. ;;   - I borrowed some stuff from 'textmode.mut'.
  29. ;;
  30. ;; History:
  31. ;;   920901   M.J. van der Velden (vdvelden@rcl.wau.nl)
  32. ;;            Public Domain (Version 1.0)
  33. ;;
  34.  
  35. (include "me2.h")
  36.  
  37. (const
  38.     TEX-FILL-COLUMN  72 ;; Column to word wrap at (0 means no wrapping)
  39.     TEX-TAB-SIZE      4 ;; Tab size (0 means use the TAB character)
  40. )
  41.  
  42. (small-int tex-fill-col)
  43.  
  44. (defun 
  45.     ;; Try to guess what the user wants based on the next keystroke.
  46.     tex-expand-backslash
  47.     {
  48.         (string what)
  49.         (int    level)
  50.         
  51.         (msg "\\")
  52.  
  53.         (switch (getchar)
  54.             "\\" {
  55.                 (insert-text "\\")
  56.             }
  57.             "\"" {
  58.                 (insert-text "``''")
  59.                 (arg-prefix 2) (previous-character)
  60.             }
  61.             "b" {
  62.                 (ask-user)
  63.                 (what (ask "\\begin What? "))
  64.                 (if (> (current-column) 1) {
  65.                     (insert-text "{\\" what " }")
  66.                     (previous-character)
  67.                 } {
  68.                     (insert-text "\\begin{" what "}^J\\end{" what "}^J")
  69.                     (previous-line)
  70.                     (open-line)
  71.                 })
  72.             }
  73.             "c" {
  74.                 (msg "\\c")
  75.                 (switch (getchar)
  76.                     "a" {
  77.                         (insert-text "\\caption{}")
  78.                         (previous-character)
  79.                     }
  80.                     "h" {
  81.                         (insert-text "\\chapter{}")
  82.                         (previous-character)
  83.                     }
  84.                     "i" {
  85.                         (insert-text "\\cite{}")
  86.                         (previous-character)
  87.                     }
  88.                 )
  89.             }
  90.             "d" {
  91.                 (insert-text "\\documentstyle{}")
  92.                 (previous-character)
  93.             }
  94.             "e" {
  95.                 (insert-text "{\\em }")
  96.                 (previous-character)
  97.             }
  98.             "f" {
  99.                 (insert-text "\\footnote{}")
  100.                 (previous-character)
  101.             }
  102.             "i" {
  103.                 (insert-text "\\item ")
  104.             }
  105.             "l" { 
  106.                 (insert-text "\\label{}")
  107.                 (previous-character)
  108.             }
  109.             "m" {
  110.                 (insert-text "\\maketitle^J")
  111.             }
  112.             "n" {
  113.                 (insert-text "\\newpage^J")
  114.             }
  115.             "p" {
  116.                 (insert-text "\\pagestyle{}")
  117.                 (previous-character)
  118.             }
  119.             "r" { 
  120.                 (insert-text "\\ref{}")
  121.                 (previous-character)
  122.             }
  123.             "s" {
  124.                 (ask-user)
  125.                 (msg "\\section Level? ")
  126.                 (level (switch (getchar) "1" 1 "2" 2 "3" 3 default 0))
  127.                 (insert-text "\\")
  128.                 (while (>= (-= level 1) 0)
  129.                     (insert-text "sub")
  130.                 )
  131.                 (insert-text "section{}^J")
  132.                 (arg-prefix 2) (previous-character)
  133.             }
  134.             "t" {
  135.                 (insert-text "\\tableofcontents^J")
  136.             }
  137.             "v" {
  138.                 (insert-text "\\verb++")
  139.                 (previous-character)
  140.             }
  141.             "$" {
  142.                 (insert-text "$$")
  143.                 (previous-character)
  144.             }
  145.         )
  146.     }
  147.    
  148.     ;; Go to the beginning of the current paragraph.
  149.     ;; If between paragraphs then go to begin of next paragraph.
  150.     tex-begin-of-paragraph
  151.     {
  152.         (beginning-of-line)
  153.         (while (and (not (EoB)) (looking-at '\ *$'))
  154.             (forward-line 1)
  155.         )
  156.         
  157.         (while (TRUE) {
  158.             (if (looking-at '\ *$') 
  159.                 (break)
  160.             )
  161.             (if (looking-at '\\[beis]')
  162.                 (break)
  163.             )
  164.             (if (not (forward-line -1))
  165.                 (break)
  166.             )
  167.         })
  168.         
  169.         (if (looking-at '\ *$')
  170.             (forward-line 1)
  171.         )
  172.     }
  173.     
  174.     ;; Go to the end of the current paragraph.
  175.     tex-end-of-paragraph
  176.     {
  177.         (beginning-of-line)
  178.         (while (looking-at '\ *$')
  179.             (forward-line 1)
  180.         )
  181.  
  182.         (while (not (EoB)) {
  183.             (forward-line 1)
  184.             (if (looking-at '\\[beis]') 
  185.                 (break)
  186.             )
  187.             (if (looking-at '\ *$') 
  188.                 (break)
  189.             )
  190.         })
  191.         
  192.         (forward-line -1)
  193.         (end-of-line)
  194.     }
  195.  
  196.     ;; Select a paragraph.    
  197.     tex-select-paragraph
  198.     {
  199.         (tex-begin-of-paragraph)
  200.         (set-mark THE-MARK)
  201.         (tex-end-of-paragraph)
  202.     }
  203.     
  204.     ;; Format a paragraph, (left justify).
  205.     ;; if wrap-solid-lines then
  206.     ;;     solid lines are wrapped
  207.     ;; endif
  208.     tex-format-paragraph ;; [BOOL wrap-solid-lines]
  209.     {
  210.         (bool      wrap-solid-lines)
  211.         (bool      at-end-of-paragraph)
  212.         (small-int right-margin)
  213.         
  214.         (wrap-solid-lines (if (== (nargs) 0) TRUE (arg 0)))
  215.         (right-margin (if (== 0 (word-wrap)) (tex-fill-column) (word-wrap)))
  216.         
  217.         (tex-begin-of-paragraph)
  218.         (at-end-of-paragraph FALSE)
  219.         (while (not at-end-of-paragraph) {
  220.             (end-of-line)
  221.             (delete-previous-whitespace)
  222.             (if (> (current-column) (right-margin)) {
  223.                 ;; a "long line"
  224.                 (current-column (+ (right-margin) 1))
  225.                 (while (and (> (current-column) 1) (previous-character)) {
  226.                     (if (is-space) {
  227.                         (next-character)
  228.                         (delete-previous-whitespace)
  229.                         (break)
  230.                     })
  231.                 })
  232.                 (if (== (current-column) 1) {
  233.                     ;; no space to put a newline
  234.                     (if wrap-solid-lines {
  235.                         (current-column (+ (right-margin) 1))
  236.                         (delete-whitespace)
  237.                         (newline)
  238.                     } {
  239.                         (forward-line 1)
  240.                     })
  241.                 } {
  242.                     (delete-whitespace)
  243.                     (newline)
  244.                 })
  245.             } {
  246.                 ;; a "short" line
  247.                 (if (not (EoB)) {
  248.                     ;; break out if we are ready
  249.                     (forward-line 1)
  250.                     (beginning-of-line)
  251.                     (if (or (looking-at '\\[beis]') (looking-at '\ *$')) {
  252.                         ;; assume \begin or \end or \it                        (at-end-of-paragraph TRUE)
  253.                     } {
  254.                         (delete-whitespace)
  255.                         (insert-text " ")
  256.                         (beginning-of-line)
  257.                         (delete-previous-character)
  258.                     })
  259.                 } {
  260.                     (at-end-of-paragraph TRUE)
  261.                 })
  262.             })
  263.         })
  264.     }
  265.  
  266.     ;; Fill (left justify) the current paragraph.    
  267.     ;; Run a second time to fill the next...
  268.     tex-fill-paragraph
  269.     {
  270.         (tex-format-paragraph FALSE)
  271.     }
  272.     
  273.     ;; Set or get the tex-fill-column.
  274.     tex-fill-column
  275.     {
  276.         (if (!= 0 (nargs)) 
  277.             (tex-fill-col (arg 0))
  278.         )
  279.         tex-fill-col
  280.     }
  281.     
  282.     tex-mode MAIN
  283.     {
  284.         (clear-modes)
  285.         
  286.         (bind-local-key "newline-and-indent"     "C-M" )
  287.         (bind-local-key "tex-end-of-paragraph"   "M-e" )
  288.         (bind-local-key "tex-begin-of-paragraph" "M-a" )
  289.         (bind-local-key "tex-select-paragraph"   "M-h" )
  290.         (bind-local-key "tex-fill-paragraph"     "M-J" )
  291.         (bind-local-key "tex-expand-backslash"   "M-\\")
  292.         (bind-local-key "tex-expand-backslash"   "C-\\")
  293.     
  294.         (major-mode "(La)TeX")
  295.         (tab-stops TEX-TAB-SIZE)
  296.         (word-wrap TEX-FILL-COLUMN)
  297.         (tex-fill-column TEX-FILL-COLUMN)
  298.     }
  299. )
  300.